/**
 * 函数信息获取接口测试用例
 *
 * Created by raquelmao on 2020/08/28.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const randomstring = require('randomstring');

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmark = require('./../../../benchmark/analyse-files/logic_bombs');

module.exports = describe('/PUT functions/:id.json', function () {
  const administrator = new Administrator();
  let otherFunctionId;
  let projectVersion = {};
  const newUserIds = [];
  function url(functionId) {
    return `/functions/${functionId}.json`;
  }

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((project) => project.createRandomVersion())
      .then((res) => {
        projectVersion = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'put');
  common.should.be.tester(uri, url, 'put');

  const description = randomstring.generate({ length: 16, charset: 'alphabetic' });
  // 正常请求
  it('it should EDIT function', function (done) {
    projectVersion.getAllFunctions(benchmark)
      .then((allFunctions) => {
        const promises = allFunctions.map((func) => {
          if (!otherFunctionId) {
            otherFunctionId = func.functionId;
          }
          const { accessToken } = projectVersion.project.creator.token;
          return new Promise((resolve, reject) => { // eslint-disable-line no-unused-vars
            chai.request(uri)
              .put(url(func.functionId))
              .set('Authorization', `Bearer ${accessToken}`)
              .query({ description })
              .end((err, res) => {
                res.should.have.status(200);
                common.should.be.functionMessage(res.body);
                res.body.description.should.be.eql(description);
                resolve();
              });
          });
        });
        return Promise.all(promises);
      })
      .then(() => {
        done();
      })
      .catch(done);
  });

  // 不可编辑不存在的函数
  it('it should edit to get function when the function is not exists', function (done) {
    new Promise((resolve) => {
      chai.request(uri)
        .put(url('000000000000000000000000'))
        .set('Authorization', `Bearer ${projectVersion.project.creator.token.accessToken}`)
        .query({ description })
        .end((err, res) => {
          res.should.have.status(404);
          common.should.be.errorMessage(res.body);
          resolve();
        });
    })
      .then(done)
      .catch(done);
  });

  // 不可编辑他人项目的函数
  it('it should edit to get function when the project is not yours', function (done) {
    let newUser;
    administrator.createRandomTester()
      .then((newTester) => {
        newUserIds.push(newTester.userId);
        newUser = newTester;
        return newTester.getToken();
      })
      .then(() => {
        chai.request(uri)
          .put(url(otherFunctionId))
          .set('Authorization', `Bearer ${newUser.token.accessToken}`)
          .query({ description })
          .end((err, res) => {
            res.should.have.status(404);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });
});
